<?php

require_once "application.php";

class RaportyController extends application {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->view->identity = $auth->getIdentity();
        }
    }
    
    public function uczenAction() {
        $uid = $this->_request->getParam('id');
        $dateod = $this->_request->getParam('date-min');
        $datedo = $this->_request->getParam('date-max');
        $from = 0;
        if($dateod>0) {
            $dtmp = split('-', $dateod);
            $from = mktime(0, 0, 0, $dtmp[1], $dtmp[2], $dtmp[0]);
        }
        //var_dump($dtmp);
        $to = time();
        if($datedo>0) {
            $dtmp = split('-', $datedo);
            $to = mktime(23, 59, 59, $dtmp[1], $dtmp[2], $dtmp[0]);
        }
        //var_dump($dtmp);
        //die('f:'.$from. ' '.$to);
        Zend_Loader::loadClass('StatLekcje');
        $stats = new StatLekcje();
        Zend_Loader::loadClass('Uzytkownik');
        $users = new Uzytkownik();
        Zend_Loader::loadClass('Screens');
        $screens = new Screens();
        Zend_Loader::loadClass('Lessons');
        $lessons = new Lessons();
        
        $user = $users->fetchRow('id='.$uid);
        if(!$user) $this->view->komunikat = 1;
        $staty = $stats->getByDate($from, $to, $uid);
        $c = count($staty);
        if($c<1) $this->_redirect ('/index/pacjent/a/zmien/id/'.$uid);
        $fp = fopen("csv/uczen_$from-$to-$uid.csv",'w');
        foreach($staty as $t) {
            $lekcja = $lessons->fetchRow('id='.$t->lekcja);
            $pola = array(
                $user->imie,
                $user->nazwisko,
                $user->birthday,
                $t->uid,
                $lekcja->name,
                $t->start,
                $t->stop,
                $t->oceniono,
                $t->dla_ucznia,
                $t->dla_opiekuna
                    
                              
            );
            fputcsv($fp, $pola);
        }
        fclose($fp);
        ///die();
        if($user) $this->_redirect("/csv/uczen_$from-$to-$uid.csv");
        
    }
    
    public function logopedaAction() {
        $id = $this->_request->getParam('id');
        $dateod = $this->_request->getParam('date-min');
        $datedo = $this->_request->getParam('date-max');
        $from = 0;
        if($dateod>0) {
            $dtmp = split('-', $dateod);
            $from = mktime(0, 0, 0, $dtmp[1], $dtmp[2], $dtmp[0]);
        }
        //var_dump($dtmp);
        $to = time();
        if($datedo>0) {
            $dtmp = split('-', $datedo);
            $to = mktime(23, 59, 59, $dtmp[1], $dtmp[2], $dtmp[0]);
        }
        //var_dump($dtmp);
        //die('f:'.$from. ' '.$to);
        Zend_Loader::loadClass('StatLekcje');
        $stats = new StatLekcje();
        Zend_Loader::loadClass('Uzytkownik');
        $users = new Uzytkownik();
        $logopeda = $users->fetchRow('id='.$id);
        Zend_Loader::loadClass('Screens');
        $screens = new Screens();
        Zend_Loader::loadClass('Lessons');
        $lessons = new Lessons();
        $userzy = $users->fetchAll('opiekun='.$id);
        $c = count($userzy);
        if($c<1) {
            $this->view->komunikat = 1;
        }
        $fp = fopen("csv/logopeda_$from-$to-$id.csv",'w');
        foreach($userzy as $u) {
            $staty = $stats->getByDate($from, $to, $u->id);

            foreach($staty as $t) {
                $lekcja = $lessons->fetchRow('id='.$t->lekcja);
                $pola = array(
                    $logopeda->imie.' '.$logopeda->nazwisko,
                    $u->imie,
                    $u->nazwisko,
                    $u->birthday,
                    $t->uid,
                    $lekcja->name,
                    $t->start,
                    $t->stop,
                    $t->oceniono,
                    $t->dla_ucznia,
                    $t->dla_opiekuna


                );
                fputcsv($fp, $pola);
            }
        }
        fclose($fp);
        ///die();
        if($c>0) $this->_redirect("/csv/logopeda_$from-$to-$id.csv");
        
    }
    
    public function placowkaAction() {
        $id = $this->_request->getParam('id');
        $dateod = $this->_request->getParam('date-min');
        $datedo = $this->_request->getParam('date-max');
        $from = 0;
        if($dateod>0) {
            $dtmp = split('-', $dateod);
            $from = mktime(0, 0, 0, $dtmp[1], $dtmp[2], $dtmp[0]);
        }
        //var_dump($dtmp);
        $to = time();
        if($datedo>0) {
            $dtmp = split('-', $datedo);
            $to = mktime(23, 59, 59, $dtmp[1], $dtmp[2], $dtmp[0]);
        }
        //var_dump($dtmp);
        //die('f:'.$from. ' '.$to);
        Zend_Loader::loadClass('StatLekcje');
        $stats = new StatLekcje();
        Zend_Loader::loadClass('Uzytkownik');
        $users = new Uzytkownik();
        //$logopeda = $users->fetchRow('id='.$id);
        Zend_Loader::loadClass('Screens');
        $screens = new Screens();
        Zend_Loader::loadClass('Lessons');
        $lessons = new Lessons();
        $userzy = $users->fetchAll('placowka='.$id);
        $c = count($userzy);
        if($c<1) {
            $this->view->komunikat = 1;
        }
        $fp = fopen("csv/placowka_$from-$to-$id.csv",'w');
        $plac = new Application_Model_Placowki();
        foreach($userzy as $u) {
            $staty = $stats->getByDate($from, $to, $u->id);

            foreach($staty as $t) {
                $placowka = $plac->fetchRow('id='.$u->placowka);
                $lekcja = $lessons->fetchRow('id='.$t->lekcja);
                $logopeda = $users->fetchRow('id='.$u->opiekun);
                $pola = array(
                    $placowka->nazwa,
                    $logopeda->imie.' '.$logopeda->nazwisko,
                    $u->imie,
                    $u->nazwisko,
                    $u->birthday,
                    $t->uid,
                    $lekcja->name,
                    $t->start,
                    $t->stop,
                    $t->oceniono,
                    $t->dla_ucznia,
                    $t->dla_opiekuna


                );
                fputcsv($fp, $pola);
            }
        }
        fclose($fp);
        ///die();
        if($c>0) $this->_redirect("/csv/placowka_$from-$to-$id.csv");
        
    }

}